/* $Id: cgpxlogger.c 4685 2008-02-17 02:57:48Z ckuethe $ */
/*
 * Copyright (c) 2005,2006 Chris Kuethe <chris.kuethe@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/cdefs.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <sstream>
#include <istream>
#include "math.h"

#include <stddef.h>
#include <dirent.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

//#include "gpsd_config.h"
//#include "gpsd.h"
#include <gps.h>

#define BS 512

#define NUM 8
char *pollstr = "SPAMDQTV\n";
char *host = "127.0.0.1";
char *port = "2947";
unsigned int sl = 1;//5; 1 second interval

struct gps_data_t *gpsdata;

char *progname;

void process(struct gps_data_t *, char *, size_t, int);
void usage(void);
void bye(int);
void html_write_record(struct gps_data_t *gpsdata);
void html_header(void);
void html_footer(void);
void html_track_start(void);
void html_track_end(void);
void write_record(struct gps_data_t *gpsdata);
void header(void);
void footer(void);
void track_start(void);
void track_end(void);
void priority_status();
void gps_update();
void signal_status();
void request_table();

int tracking = 0;
unsigned int track_count=0xffffffff;
unsigned int print_count=0;
int  is_html=0;
bool isconnected();

struct {
	double latitude;
	double longitude;
	float altitude;
	float speed;
	float course;
	float hdop;
	short svs;
	char status;
	char mode;
	char time[32];
} gps_ctx;


bool isconnected()
{
	char tempp[256];
	time_t tt0,tt1;
	long dutat;

	//std::ifstream load;
	//load.open(CONNFILE);
	//load.getline(tempp,64);
	//load.close();
	FILE * fp=fopen("/usr/local/bin/connection.txt","r");

	fscanf(fp,"%ld",&tt0);
	fclose(fp);


	tt1 = time(NULL);
	dutat = tt1-tt0;

	//printf("Time interval since last data:%ld \n",dutat);

	if (dutat>=6)
	{
		return false;
	}
	else
	{
		return true;
	}


}

int
main(int argc, char **argv){
	int ch;
	fd_set fds;
	int casoc = 0;
	int loop_count = 0;

	progname = argv[0];
	while ((ch = getopt(argc, argv, "hVwn:i:j:s:p:")) != -1){
	switch (ch) {
	case 'i':
		sl = (unsigned int)atoi(optarg);
		if (sl < 1)
			sl = 1;
		if (sl >= 3600)
			fprintf(stderr,
			    "WARNING: polling interval is an hour or more!\n");
		break;
	case 'j':
		casoc = (unsigned int)atoi(optarg);
		casoc = casoc ? 1 : 0;
		break;
	case 's':
		host = optarg;
		break;
	case 'p':
		port = optarg;
		break;
	case 'w':
	       is_html=1;
	       break;
	case 'n':
	  track_count = (unsigned int)atoi(optarg);
	  break;

	case 'V':
		(void)fprintf(stderr, "SVN ID: $Id: cgpxlogger.c 4685 2008-02-17 02:57:48Z ckuethe $ \n");
		exit(0);
	default:
		usage();
		/* NOTREACHED */
	}
	}

	gpsdata = gps_open(host, port);
	if (!gpsdata) {
		char *err_str;
		switch (errno) {
		case NL_NOSERVICE:
			err_str = "can't get service entry";
			break;
		case NL_NOHOST:
			err_str = "can't get host entry";
			break;
		case NL_NOPROTO:
			err_str = "can't get protocol entry";
			break;
		case NL_NOSOCK:
			err_str = "can't create socket";
			break;
		case NL_NOSOCKOPT:
			err_str = "error SETSOCKOPT SO_REUSEADDR";
			break;
		case NL_NOCONNECT:
			err_str = "can't connect to host";
			break;
		default:
			err_str = "Unknown";
			break;
		}
		fprintf(stderr,
		    "cgpxlogger: no gpsd running or network error: %d, %s\n",
		    errno, err_str);
		exit(1);
	}

	signal(SIGINT, bye);
	signal(SIGTERM, bye);
	signal(SIGQUIT, bye);
	signal(SIGHUP, bye);

	header();

	if (casoc)
		gps_query(gpsdata, "j1\n");

	gps_set_raw_hook(gpsdata, process);

	for(;;){
		int data;
		struct timeval tv;

		FD_ZERO(&fds);
		FD_SET(gpsdata->gps_fd, &fds);

		gps_query(gpsdata, pollstr);
		if (print_count >= track_count) {
			footer();
			exit(print_count? 0:3);
		}

		tv.tv_usec = 250000;
		tv.tv_sec = 0;

		data = select(gpsdata->gps_fd + 1, &fds, NULL, NULL, &tv);

		if (data < 0) {
			fprintf(stderr,"%s\n", strerror(errno));
			footer();
			exit(2);
		}
		else if (data) {
			gps_poll(gpsdata);
		}
		if ( print_count >= track_count || loop_count >= track_count ) {
		    	footer();
		    	exit(print_count? 0: 3);
		}

		sleep(sl);
		++loop_count;
	}
}

void usage(){
	fprintf(stderr,
	    "Usage: %s [-h] [-w ] [-n count] [-s server] [-p port] [-i interval] [-j casoc]\n\t-w => html output\n\t-n count => number of tracks to print\n",
	    progname);
	fprintf(stderr,
	    "\tdefaults to '%s -s 127.0.0.1 -p 2947 -i 5 -j 0 -n 0xffffffff'\n",
	    progname);
	exit(1);
}

void bye(int signum){
	footer();
	fprintf(stderr, "Exiting on signal %d!\n", signum);
	gps_close(gpsdata);
	exit(0);
}

void process(struct gps_data_t *gpsdata,
	     char *buf /*UNUSED*/, size_t len /*UNUSED*/, int level /*UNUSED*/){

	if ((gpsdata->fix.mode > 1) && (gpsdata->status > 0))
		write_record(gpsdata);
	else
		track_end();
}

void html_write_record(struct gps_data_t *gpsdata){
	track_start();

	printf("\t\t\t<tr>");
	if (gpsdata->fix.latitude >= 0.0)
	  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f N</td>",gpsdata->fix.latitude);
	else
	  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f S</td>",-gpsdata->fix.latitude);

	if (gpsdata->fix.longitude >= 0.0)
	  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f E</td>",gpsdata->fix.longitude);
	else
	  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f W</td>",-gpsdata->fix.longitude);

	// print altitude if we have a fix and it's 3d of some sort
	if ((gpsdata->fix.mode >= 3) && (gpsdata->status >= 1))
		printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.2f</td>", gpsdata->fix.altitude);
	else
	  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\"></td>");

	if (gpsdata->satellites_used) { // plausible timestamp
		char scr[128];
		time_t time = gpsdata->fix.time;
		ctime_r(&time,scr);
		// remove trailing /n put by ctime_r
		scr[strlen(scr)-1]=0;
		printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%s</td>",scr);
	}
	else
	  printf("<td></td>");

	//read preemption status
	//FILE * fp=fopen("/usr/local/bin/ispreemption.txt","r");
	//char aa[256];
	//fscanf(fp,"%s",aa);
	//printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%s</td>",aa);

	printf("</tr>\n");
	fflush(stdout);
}

void write_record(struct gps_data_t *gpsdata){
  ++print_count;
  if (is_html) {
    //html_write_record(gpsdata);
    return;
  }
	track_start();
	printf("      <trkpt lat=\"%.7f\" ", gpsdata->fix.latitude );
	printf("lon=\"%.7f\">\n", gpsdata->fix.longitude );

	if ((gpsdata->status >= 2) && (gpsdata->fix.mode >= 3)){
		/* dgps or pps */
		if (gpsdata->fix.mode == 4) { /* military pps */
			printf("        <fix>pps</fix>\n");
		} else { /* civilian dgps or sbas */
			printf("        <fix>dgps</fix>\n");
		}
	} else { /* no dgps or pps */
		if (gpsdata->fix.mode == 3) {
			printf("        <fix>3d</fix>\n");
		} else if (gpsdata->fix.mode == 2) {
			printf("        <fix>2d</fix>\n");
		} else if (gpsdata->fix.mode == 1) {
			printf("        <fix>none</fix>\n");
		} /* don't print anything if no fix indicator */
	}

	/* print altitude if we have a fix and it's 3d of some sort */
	if ((gpsdata->fix.mode >= 3) && (gpsdata->status >= 1))
		printf("        <ele>%.2f</ele>\n", gpsdata->fix.altitude);

	/* print # satellites used in fix, if reasonable to do so */
	if (gps_ctx.mode >= 2) {
		printf("        <hdop>%.1f</hdop>\n", gpsdata->hdop);
		printf("        <sat>%d</sat>\n", gpsdata->satellites_used);
	}

	if (gpsdata->status >= 1) { /* plausible timestamp */
		char scr[128];
		printf("        <time>%s</time>\n",
		    unix_to_iso8601(gpsdata->fix.time, scr, sizeof(scr)));
	}
	printf("      </trkpt>\n");
	fflush(stdout);
}

void html_header()
{
  char temp[64];
  char temp2[64];
  bool isconn;
  FILE * fp1=fopen("/usr/local/bin/vehicleid.txt","r");
  fscanf(fp1,"%s", temp);
  fclose(fp1);

  isconn = isconnected();
  if (isconn)
  {
	  sprintf(temp2,"CONNECTED TO A RSE!");
  }
  else
  {
	  sprintf(temp2,"SEARCHING FOR ANY RSE......");
  }
  printf("<html>\n\t<head>\n\t\t<title>");
  printf("Arizona E-VII webpage: %s </title>\n\t <H2> Signal Phase Status: %s </H2> \n\t <link href=\"styles.css\" rel=\"stylesheet\" type=\"text/css\">\n </head>\n",temp,temp2);

  printf("\t<body bgcolor=\"#000000\" text=\"#FFFFFF\">\n");
  //printf("\t\t<IMG SRC=\"DemoSite.jpg\" ALT=\"DemoSite\"> \n");




}
void header(){
  if (is_html) {
    html_header();
    return;
  }

	printf("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
	printf("<gpx version=\"1.1\" creator=\"GPX GPSD client\"\n");
	printf("        xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
	printf("        xmlns=\"http://www.topografix.com/GPX/1.1\"\n");
	printf("        xsi:schemaLocation=\"http://www.topografix.com/GPS/1/1\n");
	printf("        http://www.topografix.com/GPX/1/1/gpx.xsd\">\n");
	printf("  <metadata>\n");
	printf("    <name>GPX GPSD client</name>\n");
	printf("    <author>Chris Kuethe (chris.kuethe@gmail.com)</author>\n");
	printf("    <copyright>2-clause BSD License</copyright>\n");
	printf("  </metadata>\n");
	printf("\n");
	printf("\n");
}

void html_footer()
{
  track_end();
  //gps_update();
  signal_status();
  request_table();
  priority_status();

  //--------------Incident control ON/OFF------------------------------------------------
  printf("<FORM METHOD=POST ACTION=\"../cgi-bin/myscript.sh\" class=\"form-border\">");
  printf("<P class=\"p_indent\">Incident Indication Switch: </p>\n");
  printf("<center><INPUT TYPE=submit VALUE=\"Start Incident Message!\">");
  printf("</center>");
  printf(" </FORM>");
  printf("<FORM METHOD=POST ACTION=\"../cgi-bin/myscript2.sh\" class=\"form-border\">");
  printf("<P class=\"p_indent\">Incident Termination Switch: </p>\n");
  printf("<center><INPUT TYPE=submit VALUE=\"Stop Incident Message!\">");
  printf("</center>");
  printf("</FORM>");

  //--------------Incident information-----------------------------------------------------
  char temp[64];
  char vehid[64];
  char msg[256];
  printf("<P class=\"status\">  Current Vehicle Incident status: </P>\n");
  FILE * fp1=fopen("/usr/local/bin/isaccident.txt","r");
  fscanf(fp1,"%s", temp);
  if (strcmp(temp,"accident")==0)
	  //sprintf(msg,"Incident Active!");
	  sprintf(msg,"Broadcasting V2V Incident Warning!");
  else
	  sprintf(msg,"No Incident.");
  fclose(fp1);
  printf("<p align=\"center\" class=\"status_result\"> %s",msg);
  printf("</p>\n");

  printf("<P class=\"status\"> Other Vehicle Incident Message: </P>\n");


  double latf;
  double longf;
  double altf;
  FILE * fp=fopen("/usr/local/bin/incidentmsg.txt","r");
  while (!feof(fp))
	{
		fscanf(fp,"%s %s %lf %lf %f", temp,vehid,&latf,&longf,&altf);
		//printf(" %s %lf %lf",temp,latf,longf);
	}
  if (strcmp(temp,"incident")==0)
  	 sprintf(msg,"V2V Incident Warning Received: Incident Reported at - latitude:%lf longtitude:%lf",latf,longf);
  else
	 sprintf(msg,"No Incident Reported Active.");
  fclose(fp);
  printf("<p align=\"center\" class=\"status_result\"> %s",msg);
  printf("</p>");


  printf("\t</body>\n</html>\n");
}
void footer(){
	if (is_html) {
	  html_footer();
	  return;
	}
	track_end();
	printf("</gpx>\n");
}

void html_track_start()
{
  if (tracking != 0)
    return;
  printf("\t\t<table border=\"1\" bordercolor=\"#FFFFFF\">\n");
  //printf("\t\t\t<tr><th>Lat</th><th>Long</th><th>Alt</th><th>Time</th><th>Preemption Status</th></tr>\n");
  printf("\t\t\t<tr><th>Lat</th><th>Long</th><th>Alt</th><th>Time</th></tr>\n");
  tracking = 1;
}
void track_start(){
  if (is_html) {
    html_track_start();
    return;
  }
	if (tracking != 0)
		return;
	printf("<!-- track start -->\n  <trk>\n    <trkseg>\n");
	tracking = 1;
}

void html_track_end()
{
	if (tracking == 0)
		return;
	printf("\t\t</table>\n");
	//printf("\t\t<IMG SRC=\"DemoSite.jpg\" ALT=\"DemoSite\"> \n");
	tracking = 0;
}

void track_end(){
  if (is_html) {
    html_track_end();
    return;
  }
	if (tracking == 0)
		return;
	printf("    </trkseg>\n  </trk>\n<!-- track end -->\n");
	tracking = 0;
}

void priority_status()
{
	int isgid = 0;
	int ispreemption = 0;
	int issatisfied = 0;
	bool isconn;

	isconn = isconnected();

	FILE * fp=fopen("/usr/local/bin/isgid.txt","r");
  	fscanf(fp,"%d", &isgid);
  	fclose(fp);

  	fp=fopen("/usr/local/bin/ispreemption.txt","r");
  	fscanf(fp,"%d", &ispreemption);
	fclose(fp);

	fp=fopen("/usr/local/bin/issatisfied.txt","r");
	fscanf(fp,"%d", &issatisfied);
	fclose(fp);

	if (isgid && isconn)
	{
		printf("<table bgcolor=\"lime\" border=\"1\" align=\"center\"><tr>");
	}
	else printf("<table bgcolor=\"#202020\" border=\"1\" align=\"center\"><tr>");

	printf("<td> <font size=\"40\" color=\"black\"> GID received! </font></td>");
	printf("</tr></table>");

	if (ispreemption==1 && isconn)
	{
		printf("<table bgcolor=\"#ff0000\" border=\"1\" align=\"center\"><tr>");
		printf("<td><font size=\"40\" color=\"black\"> EV Priority request is ACTIVE----SIGNAL! </font></td>");
		printf("</tr></table>");
		printf("<table bgcolor=\"#202020\" border=\"1\" align=\"center\"><tr>");
		printf("<td><font size=\"40\" color=\"black\"> EV Priority request is ACTIVE----RAMP!</font></td>");
		printf("</tr></table>");
	}
	else if (ispreemption==2 && isconn)
	{
		printf("<table bgcolor=\"#202020\" border=\"1\" align=\"center\"><tr>");
		printf("<td><font size=\"40\" color=\"black\"> EV Priority request is ACTIVE----SIGNAL! </font></td>");
		printf("</tr></table>");
		printf("<table bgcolor=\"#ff0000\" border=\"1\" align=\"center\"><tr>");
		printf("<td><font size=\"40\" color=\"black\"> EV Priority request is ACTIVE----RAMP!</font></td>");
		printf("</tr></table>");
	}
	else
	{
		printf("<table bgcolor=\"#202020\" border=\"1\" align=\"center\"><tr>");
		printf("<td><font size=\"40\" color=\"black\"> EV Priority request is ACTIVE----SIGNAL! </font></td>");
		printf("</tr></table>");
		printf("<table bgcolor=\"#202020\" border=\"1\" align=\"center\"><tr>");
		printf("<td><font size=\"40\" color=\"black\"> EV Priority request is ACTIVE----RAMP!</font></td>");
		printf("</tr></table>");
	}

	if (issatisfied && isconn )
	{
		printf("<table bgcolor=\"yellow\" border=\"1\" align=\"center\"><tr>");

	}
	else
	{
		printf("<table bgcolor=\"#202020\" border=\"1\" align=\"center\"><tr>");
	}


	printf("<td><font size=\"40\" color=\"black\"> Request satisfied!</font></td>");
	printf("</tr></table>");



}

void gps_update()
{
	gps_query(gpsdata, pollstr);
	 printf("\t\t<table border=\"1\" bordercolor=\"#FFFFFF\">\n");
	  //printf("\t\t\t<tr><th>Lat</th><th>Long</th><th>Alt</th><th>Time</th><th>Preemption Status</th></tr>\n");
  	printf("\t\t\t<tr><th>Lat</th><th>Long</th><th>Alt</th><th>Time</th></tr>\n");

	printf("\t\t\t<tr>");
		if (gpsdata->fix.latitude >= 0.0)
		  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f N</td>",gpsdata->fix.latitude);
		else
		  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f S</td>",-gpsdata->fix.latitude);

		if (gpsdata->fix.longitude >= 0.0)
		  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f E</td>",gpsdata->fix.longitude);
		else
		  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.6f W</td>",-gpsdata->fix.longitude);

		// print altitude if we have a fix and it's 3d of some sort
		if ((gpsdata->fix.mode >= 3) && (gpsdata->status >= 1))
			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%.2f</td>", gpsdata->fix.altitude);
		else
		  printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\"></td>");

		if (gpsdata->satellites_used) { // plausible timestamp
			char scr[128];
			time_t time = gpsdata->fix.time;
			ctime_r(&time,scr);
			// remove trailing /n put by ctime_r
			scr[strlen(scr)-1]=0;
			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%s</td>",scr);
		}
		else
	  printf("<td></td>");

	  printf("\t\t</table>\n");
}

void signal_status()
{
	char sz_temp[64];
	char sz_name[64];
	int phase[8];
	int i;
	bool isconn;

	FILE * fp=fopen("/usr/local/bin/signal.txt","r");
	fscanf(fp,"%s%s%d%d%d%d%d%d%d%d", sz_temp,sz_name,&phase[0],&phase[1],&phase[2],&phase[3],&phase[4],&phase[5],&phase[6],&phase[7]);
	fclose(fp);

	isconn = isconnected();
	if (isconn)
	{
	  	printf("\t\t<table border=\"1\" bordercolor=\"#FFFFFF\">\n");
	  	printf("\t\t\t<tr><th>Signal Name</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th></tr>\n");
	  	printf("\t\t\t<tr>");

	  	printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%s</td>",sz_name);

	  	for(i=0;i<8;i++)
	  	{
	  		if (phase[i]==1)
	  		{
	  			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#00FF00\">G</td>");
	  		}
	  		else if (phase[i]==2)
	  		{
	  			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#FF8800\">Y</td>");
	  		}
	  		else
	  		{
	  			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#FF0000\">R</td>");
	  		}

	  	}

		printf("\t\t</table>\n");
	}
	else
	{
		printf("\t\t<table border=\"1\" bordercolor=\"#FFFFFF\">\n");
		printf("\t\t\t<tr><th>Signal Name</th></tr>\n");
	  	printf("\t\t\t<tr>\n");
	  	printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">NO SIGNAL AVAILABLE!</td>");
	  	printf("\t\t</table>\n");
  	}



}

void request_table()
{
	int i;
	bool isconn;

	int num_req = 0;
	char sz_temp[64];

	char VehID[64];			//  The ID of the vehicle
	int VehClass;		//	The Class of the vehicle
	int ETA;			//	The Estimation Time of Arrival
	int Phase;			//	The phase of the Intersection
	int MinGreen;		// Mini Green time to clear the queue in front of vehicle, only for 0 speed

	FILE * fp=fopen("/usr/local/bin/requests.txt","r");
	fscanf(fp,"%s%d", sz_temp,&num_req);

	isconn = isconnected();

	if (num_req>0 && isconn)
	{
		printf("<H2>Current Request Table <H2>\n");
		printf("\t\t<table border=\"1\" bordercolor=\"#FFFFFF\">\n");
		printf("\t\t\t<tr><th>Veh. ID</th><th>Veh. Class</th><th>Arrival Time</th><th>Req. Phase</th><th>Min Green</th></tr>\n");


		for(i=0;i<num_req;i++)
		{
			printf("\t\t\t<tr>");
			fscanf(fp,"%s%d%d%d%d", VehID,&VehClass,&ETA,&Phase,&MinGreen);
			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%s</td>",VehID);
			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%d</td>",VehClass);
			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%d</td>",ETA);
			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%d</td>",Phase);
			printf("<td bgcolor=\"#FFFFFF\"><font color=\"#000000\">%d</td>",MinGreen);
			printf("</tr>\n");

		}

		printf("\t\t</table>\n");

	}
	else
	{
		printf("<H2>No Active Request! <H2>\n");
	}

	fclose(fp);


}